﻿using In66.Net.Share.Rpc.Rest.Services;

namespace In66.Net.Share.Authentication
{
    public class BearerAuthenticationRemoteProcessor : AbstractAuthenticationProcessor
    {
        private IHttpContextAccessor _contextAccessor;
        private IAuthRestClient _authRestClient;
        private ILogger<BearerAuthenticationRemoteProcessor> _logger;

        public BearerAuthenticationRemoteProcessor(
            IHttpContextAccessor contextAccessor,
            IAuthRestClient authRestClient,
            ILogger<BearerAuthenticationRemoteProcessor> logger
            )
        {
            _contextAccessor = contextAccessor;
            _authRestClient = authRestClient;
            _logger = logger;
        }

        public override Task<AppInfo> GetAppInfoAsync(string appId)
        {
            throw new NotImplementedException();
        }

        //public override Task<(string ValidationVersion, int Status)> GetValidatedInfoAsync(long userId)
        //{
        //    throw new NotImplementedException();
        //}

        public override async Task<(string ValidationVersion, int Status)> GetValidatedInfoAsync(long userId)
        {
            var userContext = _contextAccessor.HttpContext.RequestServices.GetService<AuthenticationInfo>();
            userContext.ExationId = userId;

            var apiReuslt = await _authRestClient.GetValidatedInfoAsync();
            if (!apiReuslt.IsSuccessStatusCode)
            {
                var message = $"{apiReuslt.StatusCode}:{apiReuslt.Error?.Message}";
                _logger.LogDebug(message);
                return (null, 0);
            }

            return (apiReuslt.Content.ValidationVersion, apiReuslt.Content.Status);
        }
    }
}
